/*
 * Copyright 2014 by État de Genève (Switzerland)
 * All rights reserved.
 *
 * Author: ELCA Informatique SA
 */
define([
    'services/dataservice',
    'services/notifier',
    'durandal/app'
], function (ds, notifier, app) {

    var acceptAction = function () {
        this.icon = 'mi mi-status-9';
        this.name = 'Accepter';
    };

    acceptAction.prototype.doAction = function () {

        if (this.checks() && this.checks().length > 0) {
            var messageIdArray = [];

            _.each(this.checks(), function (index) {
                var item = this.items()[index];
                if (item) {
                    messageIdArray.push(item[this.primaryColumnIndex()]);
                }
            }.bind(this));

            ds.multiAcceptMessage(messageIdArray).then(triggerRefresh.bind(this));
        }
    };

    function triggerRefresh(resultWithAlerts) {
        this.globalCheck(false);
        this.checks([]);
        app.trigger('cms:menu:forceRefresh');
    }

    return acceptAction;

});